package com.mynetwork.data;

import java.io.File;

import com.mynetwork.utils.SharedPrefHelper;
import com.mynetwork.utils.Size;
import com.mynetwork.utils.SystemInfoUtils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

public class AppData {
	private static final String TAG = "AppData";
	public static final boolean sIsOrderSendToServer = false;
	
	public static String 	PATH_SDCARD = Environment.getExternalStorageDirectory().getPath();
	public static String 	SCRAP_PATH = "";
	public static String 	DATA_PATH = "";
	public static String 	ORDER_FILE_PATH = "";
	public static String 	STATISTICS_FILE_PATH = "";
	
	public static String 	PRODUCT_CONTENT_INFO_FILE_PATH = "";
	public static String 	PRODUCT_INFO_FILE_PATH = "";
	public static String 	PRODUCT_IMAGE_BACKUP_PATH = "";
	
	public static String 	WISH_ITEM_INFO_FILE_PATH = "";
	public static String 	WISH_ITEM_IMAGE_BACK_PATH = "";
	
	public static String 	CONTENT_INFO_FILE_PATH = "";
	public static String 	CONTENT_IMAGE_BACKUP_PATH = "";
	
	public static String 	BANNER_INFO_FILE_PATH = "";
	public static String 	BANNER_IMAGE_BACKUP_PATH = "";
	
	public static String 	NEWS_INFO_FILE_PATH = "";
	
	public static String 	MAGAZINE_FILE_PATH = "";
	
	
	public static String 	APP_ID;			//사용자 구분 (서버로부터 받거나, device id, 또는 mac address 사용)
	public static String 	DEVICE_ID; 		//단말 id 
	public static String	MAC_ADDRESS;
	
	public static String 	DEVICE_MODEL; //단말 모델명
	public static String 	OS_VERSION; //단말 OS버전
	public static int 		APP_VERSION; //버전 코드
	public static String	APP_VERSION_NAME = "0.0.0"; //버전 이름
	
	
	public static String 	APP_PACK_NAME; //패키지명
	
	public static float		DEVICE_DENSITY; 
	
	public final static String MAP_API_KEY = "0gVCgurvgrHdNqPZjiHCkXHvPrU_LHRFWYithTA";
	public final static String KT_MAP_API_KEY = "0uxTyNEzKkQnRe5bCLUm5ueU4zLuhMEFHSsGt3w";
	
	public static boolean IS_DEVICE_PHONE;		//target device가 폰(true)인지 테블릿(false)인지 판단
	
	public static void init(Context context) {
		IS_DEVICE_PHONE = SystemInfoUtils.isDevicePhone(context);
		
		DATA_PATH = context.getExternalFilesDir(null) + "/data";
		File file = new File(DATA_PATH);
		if (!file.exists())
			file.mkdirs();
		
		ORDER_FILE_PATH = DATA_PATH + "/order.dat";
		STATISTICS_FILE_PATH = DATA_PATH + "/statistics.dat";
		
		SCRAP_PATH = context.getExternalFilesDir(null) + "/scrap";
		
		PRODUCT_CONTENT_INFO_FILE_PATH = DATA_PATH + "/productinfo.dat";
		PRODUCT_INFO_FILE_PATH = DATA_PATH + "/productinfo.dat";
		PRODUCT_IMAGE_BACKUP_PATH = context.getExternalFilesDir(null) + "/productImgBackup";
		file = new File(PRODUCT_IMAGE_BACKUP_PATH);
		if (!file.exists())
			file.mkdirs();
		
		WISH_ITEM_INFO_FILE_PATH = DATA_PATH + "/wishinfo.dat";
		WISH_ITEM_IMAGE_BACK_PATH = context.getExternalFilesDir(null) + "/wishImgBackup";
		file = new File(WISH_ITEM_IMAGE_BACK_PATH);
		if (!file.exists())
			file.mkdirs();
		
		CONTENT_INFO_FILE_PATH = DATA_PATH + "/contentinfo.dat";
		CONTENT_IMAGE_BACKUP_PATH = context.getExternalFilesDir(null) + "/contentImgBackup";
		file = new File(CONTENT_IMAGE_BACKUP_PATH);
		if (!file.exists())
			file.mkdirs();
		
		BANNER_INFO_FILE_PATH = DATA_PATH + "/bannerinfo.dat";
		BANNER_IMAGE_BACKUP_PATH = context.getExternalFilesDir(null) + "/bannerImgBackup";
		file = new File(BANNER_IMAGE_BACKUP_PATH);
		if (!file.exists())
			file.mkdirs();
		
		NEWS_INFO_FILE_PATH = DATA_PATH + "/newsinfo.dat";
		
		MAGAZINE_FILE_PATH =  context.getExternalFilesDir(null) + "/Magazine";
		file = new File(MAGAZINE_FILE_PATH);
		if (!file.exists())
			file.mkdirs();
		
		
		DEVICE_MODEL = android.os.Build.MODEL;
		OS_VERSION = android.os.Build.VERSION.RELEASE;
		try {
			PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			APP_VERSION = pinfo.versionCode;
			APP_VERSION_NAME = pinfo.versionName;
		} catch (NameNotFoundException e) {
			Log.e(TAG, "" + e);
		}

//		DEVICE_ID = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
//		MAC_ADDRESS = getMacAddress(context);
		APP_ID = SharedPrefHelper.getAppId(context);
		APP_PACK_NAME = context.getPackageName();
		
		
		DisplayMetrics metrics = new DisplayMetrics(); 
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics); 

		DEVICE_DENSITY = metrics.density;
	}
	
	public static String getScreenSize(){
		if (Size.DisplayWidth < Size.DisplayHeight) {
			return String.format("%dx%d", Size.DisplayWidth, Size.DisplayHeight);
		} else {
			return String.format("%dx%d", Size.DisplayHeight, Size.DisplayWidth);
		}	
	}
	
	public static String getMapKey(){
		if(Config.isMarketGoogle)
			return MAP_API_KEY;
		return KT_MAP_API_KEY;	
	}
	
	/**
	 * Wifi가 꺼진 상태에서 주소를 가져오지 못하므로 wifi를 잠시 키고 주소를 얻은 후 이전 상태로 돌리기.
	 * @return Mac주소
	 */
	public static String getMacAddress(Context context){		
		String MacAddress = SharedPrefHelper.getMacAddress(context);
		if(MacAddress == null){
			//wifi 켜져 있는지 확인 후 키기
			WifiManager wfMangager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);			
			if(wfMangager == null) return "null";
			
			//mac주소 가져오기
			WifiInfo wifiInfo = wfMangager.getConnectionInfo();
			if(wifiInfo == null) return "null";
			
			MacAddress = wifiInfo.getMacAddress();
			if(MacAddress != null && MacAddress.isEmpty() == false)
			SharedPrefHelper.setMacAddress(context, MacAddress);
		}
		
		return MacAddress;
	}
	
	public static String getDeviceID(){
		if(DEVICE_ID != null){
			return DEVICE_ID;
		} else {
			return MAC_ADDRESS;
		}
	}
}
